#include "stockinfo.h"
#include <QTextCodec>
#include <QStringList>
#include <QMessageBox>

static QString fontGreenStartTag("<font color='green'>");
static QString fontRedStartTag("<font color='red'>");
static QString fontEndTag("</font>");
static QString percentSign("%");
static QString dash("--");
static QString SHID("sh000001");
static QString SZID("sz399001");

/*StockInfo::StockInfo(QByteArray &ba)
        :stockId_("")
{

}*/

StockInfo::StockInfo(QString stockId)
    :stockId_(stockId),
     currentPrice_("0"),
     isStopTrad(false)
{
    twiStockName_ = new QTableWidgetItem();
    twiStockName_->setTextAlignment(Qt::AlignHCenter);
    twiStockId_ = new QTableWidgetItem();
    twiStockId_->setTextAlignment(Qt::AlignHCenter);
    twiStockId_->setText(stockId_.right(6));
    twiCurrentPrice_ = new QTableWidgetItem();
    twiCurrentPrice_->setTextAlignment(Qt::AlignRight);
    twiDiff_ =  new QTableWidgetItem();
    twiDiff_->setTextAlignment(Qt::AlignRight);
    twiPercentage_ = new QTableWidgetItem();
    twiPercentage_->setTextAlignment(Qt::AlignRight);
    twiHighestLowest_ = new QTableWidgetItem();
    twiHighestLowest_->setTextAlignment(Qt::AlignHCenter);
    twiVolume_ = new QTableWidgetItem();
    twiVolume_->setTextAlignment(Qt::AlignRight);
}

StockInfo::~StockInfo()
{
    if(twiStockName_->tableWidget() == 0)
        delete twiStockName_;
    if(twiStockId_->tableWidget() == 0)
        delete twiStockId_;
    if(twiCurrentPrice_->tableWidget() == 0)
        delete twiCurrentPrice_;
    if(twiDiff_->tableWidget() == 0)
        delete twiDiff_;
    if(twiPercentage_->tableWidget() == 0)
        delete twiPercentage_;
    if(twiHighestLowest_->tableWidget() == 0)
        delete twiHighestLowest_;
    if(twiVolume_->tableWidget() == 0)
        delete twiVolume_;
}

QString StockInfo::getStockName()
{
    return stockName_;
}

QString StockInfo::getCurrentPrice()
{
    return currentPrice_;
}

QString StockInfo::getPercentage()
{
    if(isStopTrad)
        return "--";
    if(percentage_[0] != '-')
        percentage_="+" + percentage_;
    return percentage_ + percentSign;
}

QString StockInfo::getDiff()
{
    if(diff_[0] != '-')
        diff_="+" + diff_;
    return diff_;
}

QString StockInfo::getVolume()
{
    return volume_;
}

#include <iostream>
void StockInfo::refreshData(QByteArray &array)
{   
    allString_ = QTextCodec::codecForName("gb2312")->toUnicode(array);
    //std::cout << "what is allString_ "<<allString_.toStdString() << std::endl;
    QString tmpid = allString_.mid(11,8);

    //如果id不同则不刷新
    if(tmpid != stockId_){
        //std::cout << "error:stockid is: "<<stockId_.toStdString() << "refresh id is:" << tmpid.toStdString() << std::endl;
        return;
    }
    int start = allString_.indexOf("\"")+1;
    int end = allString_.lastIndexOf("\"");

    if( start == -1 || end == -1 || start > end)
    {
        QMessageBox::critical(0, QObject::tr("股票数据错误"),
                              QObject::tr("1股票数据错误不能刷新数据！"));
        return;
    }
    allString_ = allString_.mid(start,end-start);

    /*
            0：”大秦铁路”，股票名字；
            1：”27.55″，今日开盘价；
            2：”27.25″，昨日收盘价；
            3：”26.91″，当前价格；
            4：”27.55″，今日最高价；
            5：”26.20″，今日最低价；
            6：”26.91″，竞买价，即“买一”报价；
            7：”26.92″，竞卖价，即“卖一”报价；
            8：”22114263″，成交的股票数，由于股票交易以一百股为基本单位，所以在使用时，通常把该值除以一百；
            9：”589824680″，成交金额，单位为“元”，为了一目了然，通常以“万元”为成交金额的单位，所以通常把该值除以一万；
            10：”4695″，“买一”申请4695股，即47手；
            11：”26.91″，“买一”报价；
            12：”57590″，“买二”
            13：”26.90″，“买二”
            14：”14700″，“买三”
            15：”26.89″，“买三”
            16：”14300″，“买四”
            17：”26.88″，“买四”
            18：”15100″，“买五”
            19：”26.87″，“买五”
            20：”3100″，“卖一”申报3100股，即31手；
            21：”26.92″，“卖一”报价
            (22, 23), (24, 25), (26,27), (28, 29)分别为“卖二”至“卖四的情况”
            30：”2008-01-11″，日期；
            31：”15:05:32″，时间；
        */

    QStringList ls = allString_.split(',');

    if(ls.size() < 31){
        QMessageBox::critical(0, QObject::tr("股票数据错误"),
                              QObject::tr("2股票数据错误不能刷新数据！"));
        return;
    }

    stockName_ = ls[0];
    //std::cout << "stockname is: "<<stockName_.toStdString() << std::endl;

    //添加箭头，表示股价在上涨或下跌
    if(currentPrice_ != "0") {
        if(ls[3].toDouble() == currentPrice_.toDouble()){
            twiCurrentPrice_->setText(ls[3]);
        } else if(ls[3].toDouble() > currentPrice_.toDouble()) {
            twiCurrentPrice_->setText(QString(QObject::tr("↑"))+ls[3]);
        }else if(ls[3].toDouble() < currentPrice_.toDouble()) {
            twiCurrentPrice_->setText(QString(QObject::tr("↓"))+ls[3]);
        }
    } else {
        twiCurrentPrice_->setText(ls[3]);
    }
    currentPrice_ = ls[3];

    if( currentPrice_ == "0.00"){ // 停牌的情况
        isStopTrad = true;
        diff_ = dash;
        highest_ = dash;
        lowest_ = dash;
        volume_ = dash;
    }else {
        diff_ = QString::number(ls[3].toDouble()-ls[2].toDouble());
        percentage_ = QString::number((ls[3].toDouble()-ls[2].toDouble())/ls[2].toDouble()*100,'f',2);
        highest_ = ls[4];
        lowest_ = ls[5];
        volume_= ls[9];
    }
    twiStockName_->setText(stockName_);
    twiStockId_->setText(stockId_.right(6));

    twiDiff_->setText(getDiff());
    twiPercentage_->setText(getPercentage());
    twiHighestLowest_->setText(highest_+QString("/")+lowest_);

    if(stockId_ == SHID){
        twiVolume_->setText(volume_.left(volume_.length()-8));
        //twiVolume_->setText(volume_.left(3));
        //QString str = volume_.setNum(volume_.toDouble());
        //twiVolume_->setText(str);
    }else if(stockId_ == SZID){
        int v = volume_.length() - volume_.indexOf(".");
        twiVolume_->setText(volume_.left(volume_.length()-8-v));
    }else {
        twiVolume_->setText(volume_.left(volume_.length()-4));
    }

    addColor();
}

void StockInfo::addColor()
{
    if(isStopTrad)
        return;

    if(percentage_[0] == '-'){
        twiCurrentPrice_->setTextColor(Qt::green);
        twiDiff_->setTextColor(Qt::green);
        twiPercentage_->setTextColor(Qt::green);
        twiHighestLowest_->setTextColor(Qt::green);
        twiVolume_->setTextColor(Qt::green);
    } else {
        twiCurrentPrice_->setTextColor(Qt::red);
        twiDiff_->setTextColor(Qt::red);
        twiPercentage_->setTextColor(Qt::red);
        twiHighestLowest_->setTextColor(Qt::red);
        twiVolume_->setTextColor(Qt::red);
    }
}

QTableWidgetItem* StockInfo::getTWIStockName()
{
    return twiStockName_;
}

QTableWidgetItem* StockInfo::getTWIStockId()
{
    return twiStockId_;
}

QTableWidgetItem* StockInfo::getTWICurrentPrice()
{
    return twiCurrentPrice_;
}

QTableWidgetItem* StockInfo::getTWIPercentage()
{
    return twiPercentage_;
}

QTableWidgetItem* StockInfo::getTWIDiff()
{
    return twiDiff_;
}

QTableWidgetItem* StockInfo::getTWIHighestLowest()
{
    return twiHighestLowest_;
}

QTableWidgetItem* StockInfo::getTWIVolume()
{
    return twiVolume_;
}
